package com.mall.controller;

import com.mall.pojo.Admin;
import com.mall.pojo.Business;
import com.mall.pojo.Commodity;
import com.mall.pojo.Good;
import com.mall.service.AdminService;
import com.mall.service.BusinessService;
import com.mall.util.DateTool;
import com.mall.vo.Result;
import com.sun.rowset.internal.SyncResolverImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
public class BusinessController {

    @Autowired
    private BusinessService businessService;
    @Autowired
    private AdminService adminService;

    @Value("${file.upload.root-path}")
    private String rootPath;



    @RequestMapping("/back/business/upload")
    public Result upload(MultipartFile file){

        System.out.println("到达文件上传");
        System.out.println(rootPath);
        String originalFileName = file.getOriginalFilename();
        String[] namesArr = originalFileName.split("\\.");
        String suffix = namesArr[namesArr.length-1];
        String timePath = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd/"));
        System.out.println(timePath+"---");

        File pathFile = new File(rootPath+timePath);

        //文件夹不存在时自动创建
        if(!pathFile.exists()){
            pathFile.mkdirs();

        }
        String fileName = UUID.randomUUID().toString()+"."+suffix;
        try{
            file.transferTo(new File(rootPath+timePath +fileName));
            Result result = new Result();
            result.setCode(1);
            result.setMessage("上传成功");
            result.getDataMap().put("path", timePath+fileName);
            System.out.println(result);
            System.out.println("yes");
            return  result;

        }catch (IOException e){
            System.out.println("null");
            return null;

        }


    }


    @RequestMapping("/back/business/reg")
    public Result reg(String storeName, String storeKeeper,String identityCard,String phone,String pwd){
        System.out.println(storeName+"---"+storeKeeper+"---"+identityCard+"---"+phone+"--"+pwd);

        String date = DateTool.getDate(new Date());
        System.out.println(date+"--------date");

        Business business = new Business(0,storeName,storeKeeper,pwd,identityCard,phone,date,null,0);



        Boolean res = businessService.insertBusiness(business);


//        System.out.println(admin);
        Result result = new Result();

        if (res) {
            result.setCode(1);
            result.setMessage("注册成功");
        } else {
            result.setCode(0);
            result.setMessage("注册失败");

        }

        return  result;
    }





    @RequestMapping("back/business/businessLogin") //方法注解  具体地址：http://localhost:8080/back/teacher/showList
    public Result businessLogin(String identityCard, String pwd, HttpSession httpSession) {

        System.out.println(identityCard + "__________" + pwd);

        int identityCardLen = identityCard.length();
        System.out.println("identityCard的====长度是"+identityCardLen);

//        String code = (String) httpSession.getAttribute("captchaCode");
//        System.out.println("前端发过来的验证码是"+captCha);
//        System.out.println("后端正确的验证码是"+code);


        Result result = new Result();

//        if(code==null || !code.equalsIgnoreCase(captCha)){
//            result.setCode(3);
//            result.setMessage("验证码错误");
//            System.out.println("验证码错误======================================================");
//            return JSON.toJSONString(result);
//        }else{
//            System.out.println("验证码正确======================================================");
//        }

//        password = Md5Tool.md5(password);


        if(identityCardLen>15){
            System.out.println("身份证登录！！！原来========================");
            Business business = businessService.login(identityCard, pwd);
            if (business == null) {
                result.setCode(0);
                result.setMessage("账号密码有误");
            } else {
                result.setCode(1);
                result.setMessage("商家身份证号登录成功！");


                httpSession.setAttribute("BusinessId", business.getBusinessId());
                Object staffId = httpSession.getAttribute("BusinessId");
                System.out.println(staffId+ "--BusinessId---我收到了");
                result.getDataMap().put("business", business);

            }

            return result;  //响应给前端的内容

        }else {
            System.out.println("手机号码登录！！！========================");
            Business business = businessService.loginByPhone(identityCard, pwd);
            if (business == null) {
                result.setCode(0);
                result.setMessage("账号密码有误");
            } else {
                result.setCode(11);
                result.setMessage("商家手机号登录成功！");


                httpSession.setAttribute("BusinessId", business.getBusinessId());
                Object staffId = httpSession.getAttribute("BusinessId");
                System.out.println(staffId+ "--BusinessId---我收到了");
                result.getDataMap().put("business", business);

            }

            return result;  //响应给前端的内容
        }


    }






    @RequestMapping("/back/business/showCommodity")
    public Result showCommodity(String name,Integer pageSize, Integer offset){
        System.out.println(name);
        System.out.println(pageSize);
        System.out.println(offset);
        System.out.println("展示");


        List<Commodity> commodityList = businessService.showCommodity(name,pageSize,offset);
        System.out.println(commodityList);

        Integer totalSearch =  businessService.showCommodityCount(name);
        System.out.println(totalSearch+"----");

        List<Good> goodAllList =  adminService.searchAll();

        Result result = new Result();

        result.setCode(1);
        result.setMessage("搜索商品成功");
        result.getDataMap().put("commodityList", commodityList);
        result.getDataMap().put("goodAllList", goodAllList);
        result.getDataMap().put("totalSearch", totalSearch);

        return result;  //响应给前端的内容
    }





    @RequestMapping("/back/business/addCommodity")
    public Result addCommodity(String commodityName,Double price,String stock,String goodName,String description,String imgPath){
        System.out.println(commodityName);
        System.out.println(price);
        System.out.println(stock);
        System.out.println(goodName);
        System.out.println(description);
        System.out.println(imgPath);


        Commodity commodity = new Commodity(null,commodityName,stock,null,1,description,price,imgPath,goodName);


        Boolean res = businessService.addCommodity(commodity);



        //alt  回车  生成方法！
        Result result = new Result();
        if (res) {
            result.setCode(1);
            result.setMessage("新增成功");
        } else {
            result.setCode(0);
            result.setMessage("新增失败");

        }


        return result;  //响应给前端的内容
    }








}
